/* eslint-disable no-undef */
import { errorMapper } from "./modules/errorMapper";
// import { sayHello } from './modules/utils'
import { roleHarvester } from "./modules/role/harvester";
import { roleUpgrader } from "./modules/role/upgrader";
import { roleBuilder } from "./modules/role/builder";
import { initGlobalValue, GlobalValue } from "./modules/utils";

export const loop = errorMapper(() => {
    for (const name in Memory.creeps) {
        if (!Game.creeps[name]) {
            delete Memory.creeps[name];
            console.log("Clearing non-existing creep memory:", name);
        }
    }

    initGlobalValue();

    // 找Tower
    const towertargets = GlobalValue.structures.filter((structure) => {
        if (structure.structureType === STRUCTURE_TOWER) {
            return true;
        } else {
            return false;
        }
    });
    console.log("--------------------------------")
    console.log("Towertargets length:", towertargets.length)
    for (const tower of towertargets) {
        console.log("Tower information:", tower.id, tower.structureType, (tower as StructureTower).store)
    }
    if (towertargets.length > 0) {
        const closestHostile =
            towertargets[0].pos.findClosestByRange(FIND_HOSTILE_CREEPS);
        if (closestHostile != null) {
            // 让所有塔攻击
            for (const tower of towertargets) {
                (tower as StructureTower).attack(closestHostile);
            }
        }
    }


    const harvesters = _.filter(
        Game.creeps,
        (creep) => creep.memory.role === "harvester"
    );
    console.log("Harvesters: " + harvesters.length.toString());
    const upgraders = _.filter(
        Game.creeps,
        (creep) => creep.memory.role === "upgrader"
    );
    console.log("upgraders: " + upgraders.length.toString());
    const builders = _.filter(
        Game.creeps,
        (creep) => creep.memory.role === "builder"
    );
    console.log("builders: " + builders.length.toString());

    if (harvesters.length < 2) {
        let newName = "HarvesterBig" + Game.time.toString();
        let result = Game.spawns.Spawn1.spawnCreep(
            [WORK, WORK, WORK, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE],
            newName,
            { memory: { role: "harvester" } }
        );
        if (result === ERR_NOT_ENOUGH_ENERGY) {
            // 能量不够，用基本的代替
            newName = "Harvester" + Game.time.toString();
            result = Game.spawns.Spawn1.spawnCreep(
                [WORK, CARRY, MOVE],
                newName,
                {
                    memory: { role: "harvester" }
                }
            );
        }
        console.log("尝试抚养harvester：" + result.toString());
    }
    if (upgraders.length < 2) {
        let newName = "UpgraderBig" + Game.time.toString();

        let result = Game.spawns.Spawn1.spawnCreep(
            [WORK, WORK, WORK, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE],
            newName,
            { memory: { role: "upgrader" } }
        );
        if (result === ERR_NOT_ENOUGH_ENERGY) {
            // 能量不够，用基本的代替
            newName = "Upgrader" + Game.time.toString();
            result = Game.spawns.Spawn1.spawnCreep(
                [WORK, CARRY, MOVE],
                newName,
                {
                    memory: { role: "upgrader" }
                }
            );
        }
        console.log("尝试抚养upgrader：" + result.toString());
    }
    if (builders.length < 1) {
        let newName = "BuilderBig" + Game.time.toString();

        let result = Game.spawns.Spawn1.spawnCreep(
            [WORK, WORK, WORK, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE],
            newName,
            { memory: { role: "builder" } }
        );
        if (result === ERR_NOT_ENOUGH_ENERGY) {
            // 能量不够，用基本的代替
            newName = "Builder" + Game.time.toString();
            result = Game.spawns.Spawn1.spawnCreep(
                [WORK, CARRY, MOVE],
                newName,
                {
                    memory: { role: "builder" }
                }
            );
        }
        console.log("尝试抚养builder：" + result.toString());
    }

    if (Game.spawns.Spawn1.spawning) {
        const spawningCreep = Game.creeps[Game.spawns.Spawn1.spawning.name];
        Game.spawns.Spawn1.room.visual.text(
            "🛠️" + spawningCreep.memory.role,
            Game.spawns.Spawn1.pos.x + 1,
            Game.spawns.Spawn1.pos.y,
            { align: "left", opacity: 0.8 }
        );
    }

    for (const name in Game.rooms) {
        console.log(
            'Room "' +
            name +
            '" has ' +
            Game.rooms[name].energyAvailable.toString() +
            " energy"
        );
    }

    for (const name in Game.creeps) {
        const creep = Game.creeps[name];
        if (creep.memory.role === "harvester") {
            roleHarvester.run(creep);
        } else if (creep.memory.role === "upgrader") {
            roleUpgrader.run(creep);
        } else if (creep.memory.role === "builder") {
            roleBuilder.run(creep);
        }
    }
});
